package cn.kgc.ebookmanagement.service.impl;

import cn.kgc.ebookmanagement.mapper.EbookEntryMapper;
import cn.kgc.ebookmanagement.pojo.EbookEntry;
import cn.kgc.ebookmanagement.pojo.EbookEntryExample;
import cn.kgc.ebookmanagement.service.EbookEntryService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author shkstart
 * @create 2020-05-18 18:03
 */
@Service
public class EbookEntryServiceImpl implements EbookEntryService {

    @Resource
    EbookEntryMapper ebookEntryMapper;


    @Override
    public PageInfo<EbookEntry> select(Integer pageNum, Integer pageSize, Integer categoryId) {
        PageHelper.startPage(pageNum,pageSize);
        List<EbookEntry> entryList;
        if (categoryId!=0){
            EbookEntryExample example=new EbookEntryExample();
            EbookEntryExample.Criteria criteria=example.createCriteria();
            criteria.andCategoryidEqualTo(categoryId);
            entryList=ebookEntryMapper.selectByExample(example);
        }else{
            entryList=ebookEntryMapper.selectByExample(null);
        }

        PageInfo<EbookEntry>pageInfo=new PageInfo<>(entryList);
        return pageInfo;
    }

    @Override
    public Integer add(EbookEntry ebookEntry) {
        int i=ebookEntryMapper.insert(ebookEntry);
        return i;
    }

    @Override
    public EbookEntry selectById(Integer id) {
        EbookEntry ebookEntry=ebookEntryMapper.selectByPrimaryKey(id);
        return null;
    }

    @Override
    public Integer update(EbookEntry ebookEntry) {
        int i=ebookEntryMapper.updateByPrimaryKeySelective(ebookEntry);
        return i;
    }

    @Override
    public Integer delete(Integer id) {
        int i=ebookEntryMapper.deleteByPrimaryKey(id);
        return i;
    }
}
